package io.xxx.xbutler.common;

import lombok.Data;

@Data
public class ApiResult {

    private static final ApiResult INSTANCE = new ApiResult(null);

    private int code;

    private String message;

    private Object data;

    private ApiResult(Object data) {
        this.data = data;
    }

    private ApiResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ApiResult success() {
        return INSTANCE;
    }

    public static ApiResult success(Object data) {
        return new ApiResult(data);
    }

    public static ApiResult fail(int code, String message) {
        return new ApiResult(code, message);
    }

    public static ApiResult fail(Throwable t) {
        return new ApiResult(-1, t.getLocalizedMessage());
    }

    public static ApiResult fail(ApplicationException e) {
        return new ApiResult(e.getCode(), e.getMessage());
    }
}
